This package was created by Diego Valle-Jones.
mxmaps is based on choroplethr (thematic maps where geographic regions, such as states, are colored according to some metric) and can be used to easily create maps of Mexico at both the state and municipality levels.
It also includes functions to create interactive maps using the leaflet package, map INEGI data from its API, and use your own data to create maps of Mexico.
The latter can be achieve by formatting strings so they match the INEGI state and municipio codes.
For more information visit: https://www.diegovalle.net/mxmaps/
You can install the package from github:
if (!require("devtools")) {
install.packages("devtools")
}
devtools::install_github("diegovalle/mxmaps")
There are two types of maps within this package:
Static
Leaflet
The choropleth map is part of the static group. We’re going to use the States and the Municipios choropleths.
There is also a Hexbins map (but is not really that pretty). You’ll have a chance to look at the code later on.
# to load the package
library("mxmaps")
df_mxstate$value <- df_mxstate$pop
mxstate_choropleth(df_mxstate,
title = "Total population, by state")
Looking at the syntax
In this case, our object is called df_mxstate because we are using the data from the package (of the same name). We could change the name of our object, but what we cannot change is value (marked in red). If we did change it, the code wouldn’t work.
Next we need to select the column we want to plot. In this case, we’re plotting the population column contained in the df_mxmaps data set.
We could try plotting something else, but first let’s take a look at the data in df_mxmaps:
# To look at the entire table
# View(df_mxstate)
# To get the column names
colnames(df_mxstate)
## [1] "region" "state_name" "state_name_official"
## [4] "state_abbr" "state_abbr_official" "pop"
## [7] "pop_male" "pop_female" "afromexican"
## [10] "part_afromexican" "indigenous" "part_indigenous"
## [13] "value"
Now, let’s try plotting the number of female inhabitants per state:
df_mxstate$value <- df_mxstate$pop_female
mxstate_choropleth(df_mxstate,
title = "Female population, by state")
This is how the data looks like:
# install.packages("tidyverse")
library(tidyverse)
fem_pop <- df_mxstate[,c("state_name", "pop_female")] %>%
arrange(desc(df_mxstate$pop_female))
head(fem_pop)
## state_name pop_female
## 1 México 8353540
## 2 Ciudad de México 4687003
## 3 Veracruz 4203365
## 4 Jalisco 4009761
## 5 Puebla 3225206
## 6 Guanajuato 3027308
We can also plot Mexican municipipalities using the dataframe df_mxmunicipios with a continuous color scale set with num_colors = 1:
data("df_mxmunicipio")
df_mxmunicipio$value <- df_mxmunicipio$indigenous / df_mxmunicipio$pop * 100
mxmunicipio_choropleth(df_mxmunicipio,
num_colors = 1,
title = "Percentage of the population that self-identifies\ as indigenous",
legend = "%")
If we take a look at the df_mxmunicipio dataset we’ll see we have additional information, such as the metro area the municipality belongs to (although not available for all of them).
colnames(df_mxmunicipio)
## [1] "state_code" "municipio_code" "region"
## [4] "state_name" "state_name_official" "state_abbr"
## [7] "state_abbr_official" "municipio_name" "pop"
## [10] "pop_male" "pop_female" "afromexican"
## [13] "part_afromexican" "indigenous" "part_indigenous"
## [16] "metro_area" "long" "lat"
## [19] "value"
Now, try plotting the same data changing the number of colors (num_colors) and see what happens.
To focus on a certain area, we can use the zoom parameter.
mxmunicipio_choropleth(df_mxmunicipio, num_colors = 1,
zoom = subset(df_mxmunicipio, metro_area %in% c("Monclova-Frontera",
"Guaymas",
"Piedras Negras",
"Saltillo",
"Monterrey",
"Chihuahua",
"Juárez",
"Matamoros",
"Tampico",
"Reynosa",
"Mexicali",
"Tijuana"))$region,
title = "Percentage of the population that self-identifies\ as indigenous in the northern states",
legend = "%")
We can also use the show_states parameter to hide or show state borders when making municipio choropleths.
mxmunicipio_choropleth(df_mxmunicipio, num_colors = 1,
zoom = subset(df_mxmunicipio, state_name %in% c("Yucatán", "México"))$region,
title = "Percentage of the population that self-identifies\ as indigenous in Yucatán and Estado de Mexico",
show_states = FALSE,
legend = "%")
Here have a quick look at how to plot a Hexbins map:
data("df_mxstate")
df_mxstate$value = df_mxstate$afromexican / df_mxstate$pop * 100
mxhexbin_choropleth(df_mxstate, num_colors = 1,
title = "Percentage of the population that self-identifies as Afro-Mexican",
legend = "%")
Notice that this option shows labels for each state. For the States map you can also add this feature. Another thing we can do is change the color scale.
We can use the viridis color scale, famous for its vibrant tones. To use it we need to install additional packages.
library(viridis)
library(scales)
df_mxstate$value <- df_mxstate$indigenous / df_mxstate$pop
gg = MXStateChoropleth$new(df_mxstate)
gg$title <- "Percentage of the population that self-identifies as indigenous"
gg$set_num_colors(1)
gg$ggplot_scale <- scale_fill_viridis("percent", labels = percent)
gg$render()
library(leaflet)
library(scales)
df_mxstate$value <- df_mxstate$indigenous / df_mxstate$pop
pal <- colorNumeric("Blues", domain = df_mxstate$value)
mxstate_leaflet(df_mxstate,
pal,
~ pal(value),
~ sprintf("State: %s<br/> Percent of Indigenous Population: %s%%",
state_name, round(value * 100, 1))) %>%
addLegend(position = "bottomright",
pal = pal,
values = df_mxstate$value,
title = "Percent<br>Indigenous Population",
labFormat = labelFormat(suffix = "%",
transform = function(x) {100 * x})) %>%
addProviderTiles("CartoDB.Positron") # removes water and additional objects, you can remove it and see what happens
Now, try clicking on any of the states and see what happens.
And, if you try to zoom in or zoom out?
Playing with the color scheme
If you want to make a “pretty” plot, you can try and change the color scheme.
Here we’ll use the municipalities data and the Viridis cholor scheme.
library(leaflet)
df_mxmunicipio$value <- df_mxmunicipio$pop_female / df_mxmunicipio$pop
# The magma object contains the Viridis color scheme
magma <- c("#000004FF", "#1D1146FF", "#50127CFF", "#822681FF", "#B63779FF",
"#E65163FF", "#FB8761FF", "#FEC387FF", "#FCFDBFFF")
pal <- colorNumeric(magma, domain = df_mxmunicipio$value)
mxmunicipio_leaflet(df_mxmunicipio,
pal,
~ pal(value),
~ sprintf("State: %s<br/>Municipio : %s<br/>Percent Female Population: %s%%",
state_name, municipio_name, round(value * 100, 1))) %>%
addLegend(position = "topright",
pal = pal,
values = df_mxmunicipio$value,
title = "Percent<br>Female Population",
labFormat = labelFormat(suffix = "%",
transform = function(x) {100 * x})) %>%
addProviderTiles("CartoDB.Positron")
You can try applying this color scheme to the df_mxstate dataframe OR create your own color scheme!
You can find more Hex color codes here: https://htmlcolorcodes.com/